home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / CUGUK / PROG_TOO / C027B.ZIP / LIBFP / FPCONV.C < prev    next >
Text File  |  1990-03-30  |  1KB  |  95 lines

  1. /* Copyright (c) 1988 by Sozobon, Limited.  Author: Johann Ruegg
  2.  *
  3.  * Permission is granted to anyone to use this software for any purpose
  4.  * on any computer system, and to redistribute it freely, with the
  5.  * following restrictions:
  6.  * 1) No charge may be made other than reasonable charges for reproduction.
  7.  * 2) Modified versions must be clearly marked as such.
  8.  * 3) The authors are not responsible for any harmful consequences
  9.  *    of using this software, even if they result from defects in it.
  10.  */
  11.  
  12. long fpltof(l)
  13.     long l;
  14.     {
  15.     int s;
  16.     long m, fputof();
  17.  
  18.     if (l < 0)
  19.         {
  20.         s = 0x80;
  21.         l = -l;
  22.         }
  23.     else
  24.         s = 0;
  25.  
  26.     m = fputof(l) | s;
  27.     return m;
  28.     }
  29.  
  30. long fputof(l)
  31.     long l;
  32.     {
  33.     register int e;
  34.     register long m;
  35.  
  36.     m = l;
  37.     if (m == 0)
  38.         return 0;
  39.  
  40.     e = 0x40 + 32;
  41.     while (m > 0)
  42.         {
  43.         m <<= 1;
  44.         e--;
  45.         }
  46.  
  47.     return (m & ~0xff) | e;
  48.     }
  49.  
  50. #define MIN_L    0x80000000
  51. #define MAX_L    0x7fffffff
  52. #define MAX_U    0xffffffff
  53.  
  54. long fpftol(f)
  55.     register long f;
  56.     {
  57.     long m, fpftou();
  58.  
  59.     if (f & 0x80)
  60.         {
  61.         f ^= 0x80;
  62.         m = fpftou(f);
  63.         if (m < 0)
  64.             return MIN_L;
  65.         return -m;
  66.         }
  67.     else
  68.         {
  69.         m = fpftou(f);
  70.         if (m < 0)
  71.             return MAX_L;
  72.         return m;
  73.         }
  74.     }
  75.  
  76. long fpftou(f)
  77.     long f;
  78.     {
  79.     register unsigned long m;
  80.     register e;
  81.  
  82.     m = f & ~0xff;
  83.     e = f & 0x7f;
  84.     if (e < 0x40 || m == 0)        /* underflow */
  85.         return 0;
  86.  
  87.     if (e > 0x40 + 32)        /* overflow */
  88.         return MAX_U;
  89.  
  90.     e -= 0x40;
  91.  
  92.     m >>= 32 - e;
  93.     return m;
  94.     }
  95.